package com.utils
{
	import com.org.greensock.TweenLite;
	import com.org.greensock.easing.Back;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	public class Alert extends Sprite
	{
		public static const alert:Alert = new Alert();
		public static const YES:int = 0;
		public static const NO:int = 1;
		public static var neverNoTipFlag:Boolean = false;
		
		private var _parentContain:DisplayObjectContainer;
		private var _stage:Stage;
		private var _bgPath:String;//背景图片路径
		private var _btnPath:String;//按钮图片路径
		private var _gridPath:String;//背景图片路径
		private var _tickPath:String;//按钮图片路径
		private var _btnY:int;//按钮Y轴坐标
		private var _textY:int;//文本Y轴坐标
		private var _textWidth:int;//文本最大宽度
		private var _textHeight:int;//文本最大高度
		private var _noTipBoxY:int;
		private var _alertPanel:AlertPanel;
		private var _bgBitmapData:BitmapData;
		private var _btnBitmapData:BitmapData;
		private var _gridBitmapData:BitmapData;
		private var _tickBitmapData:BitmapData;
		private var _loader:Loader;
		private var _loadList:Array = [];
		private var _curFilePath:String;
		private var _infoTextStr:String;//显示内容
		private var _showNoTipBox:Boolean = false;
		
		private var _clickCallBack:Function = new Function();//点击回调
		public function Alert()
		{
			if(alert) throw Error('不能重复实例化!');
		}
		
		/**
		 * 注册父容器和舞台 
		 */		
		public function register(parent:DisplayObjectContainer,stage:Stage):void
		{
			_parentContain = parent;
			_stage = stage;
		}
		
		/**使用本地图片做界面*/		
		public function useLocalSkin():void
		{
			_bgPath = 'assets/images/提示框.png';
			_btnPath = 'assets/images/AlertBtn.png';
			_gridPath = 'assets/images/AlertGrid.png';
			_tickPath = 'assets/images/AlertTick.png';
			_btnY = 130;
			_textY = 15;
			_textWidth = 330;
			_textHeight = 100;
			_noTipBoxY = 10;
			_loadList = [_bgPath,_btnPath,_gridPath,_tickPath];
			if( !_loader) _loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
			loopLoad()
		}
		
		private function loopLoad():void
		{
			_curFilePath = _loadList.pop();
			_loader.load(new URLRequest(_curFilePath));
		}
		
		protected function onCompleteHandler(e:Event):void
		{
			switch(_curFilePath){
				case _bgPath:
					_bgBitmapData = Bitmap(e.target.content).bitmapData;
					break;
				case _btnPath:
					_btnBitmapData = Bitmap(e.target.content).bitmapData;
					break;
				case _gridPath:
					_gridBitmapData = Bitmap(e.target.content).bitmapData;
					break;
				case _tickPath:
					_tickBitmapData = Bitmap(e.target.content).bitmapData;
					break;
			}
			if(_loadList.length == 0){
				_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onCompleteHandler);  
				_loader.unload();
				_loader = null;
				trace('图片加载完毕!');
			}else{
				loopLoad();
			}
		}
		
		protected function onErrorHandler(event:IOErrorEvent):void
		{
			trace('图片路径出错：' +event.text);
		}		
		
		public function showYesMsg(infoStr:String,clickCallBack:Function=null,showNoTipBox:Boolean = false):void{
			_clickCallBack = clickCallBack;
			_showNoTipBox = showNoTipBox;
			_infoTextStr = infoStr;
			showPanel();
		}
		
		public function showYesNoMsg(infoStr:String,clickCallBack:Function=null,showNoTipBox:Boolean = false):void{
			_clickCallBack = clickCallBack;
			_showNoTipBox = showNoTipBox;
			_infoTextStr = infoStr;
			showPanel(false);
		}
		
		private function showPanel(isOneBtn:Boolean = true):void{
			if(_alertPanel) clear(_alertPanel);
			_alertPanel = new AlertPanel(_bgBitmapData,_btnBitmapData,_gridBitmapData,_tickBitmapData,_btnY,_textY,_textWidth,_textHeight,_noTipBoxY,_infoTextStr,isOneBtn,_showNoTipBox);
			_alertPanel.clickBtnCallBack = clickBtnCallBack;
			_alertPanel.x= _stage.stageWidth/2;
			_alertPanel.y= _stage.stageHeight/2;
			_alertPanel.scaleX = _alertPanel.scaleY = 0;
			_parentContain.addChild(_alertPanel);
			_alertPanel.alpha = 0;
			TweenLite.to(_alertPanel,.3,{scaleX:1,scaleY:1,alpha:1,ease:Back.easeOut});
		}
		
		private function clickBtnCallBack(btnType:int,target:AlertPanel):void
		{
			if(_clickCallBack is Function) _clickCallBack(btnType);
			clearPanel(target);
		}		
		
		private function clearPanel(target:AlertPanel):void{
			TweenLite.to(target,.2,{scaleX:.3,scaleY:.3,onComplete:clear,onCompleteParams:[target],ease:Back.easeIn});
		}
		/**清理*/		
		private function clear(target:AlertPanel):void{
			target.clear();
			target = null;
		}
		
	}
}

import com.utils.ButtonModule;
import com.utils.TextFieldUtil;
import com.utils.Alert;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;

class AlertPanel extends Sprite
{
	private var _textinfoStr:String = '';
	private var _textInfo:TextField; 
	private var _alertBg:DisplayObject;
	private var _yesBtn:ButtonModule;
	private var _noBtn:ButtonModule;
	private var _noTipBox:NoTipBox;
	private var _bgHeigth:int = 180;
	private var _bgWidth:int = 330;
	private var _btnY:int = 150;
	private var _textY:int = 20;
	private var _textWidth:int = 300;
	private var _textHeight:int = 100;
	private var _noTipBoxY:int = 35;
	
	private var _isOneBtn:Boolean = true;
	private var _clickBtnCallBack:Function = new Function();
	private var _btnType:int;
	private var _showNoTipBox:Boolean = false;
	public function AlertPanel(bgBmd:BitmapData,btnBmd:BitmapData,gridBmd:BitmapData,tickBmd:BitmapData,btnY:int,textY:int,textWidth:int,textHeight:int,noTipBoxY:int,textinfoStr:String,isOneBtn:Boolean=true,showNoTipBox:Boolean = true)
	{
		_textinfoStr = textinfoStr;
		_isOneBtn = isOneBtn;
		_showNoTipBox = showNoTipBox;
		if(bgBmd){
			_btnY = btnY;
			_textY = textY;
			_textWidth = textWidth;
			_textHeight = textHeight;
			_noTipBoxY = noTipBoxY;
			_alertBg = new Bitmap(bgBmd);
			_yesBtn = new ButtonModule(new Bitmap(btnBmd),['确定'],{color:'#F5E49F',filters:[new GlowFilter(0, 1, 2, 2, 4, 1)]},null);
			_noBtn = new ButtonModule(new Bitmap(btnBmd),['取消'],{color:'#F5E49F',filters:[new GlowFilter(0, 1, 2, 2, 4, 1)]},null);
		}else{
			_alertBg = new DrawShape(_bgWidth,_bgHeigth);
			_yesBtn = new ButtonModule(null,['确定'],null,{height:25});
			_noBtn = new ButtonModule(null,['取消'],null,{height:25});
		}
		_noTipBox = new NoTipBox(gridBmd,tickBmd);
		this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		this.filters = [new GlowFilter(0,.8,10,10,1)];
	}
	
	/**添加到舞台调用 */		
	private function onAddToStageHandler(e:Event):void
	{
		this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		_isOneBtn ? showYes() : showYesNo();
		this.addChild(_noTipBox);
		_noTipBox.y = _noTipBoxY;
		_noTipBox.x = -_noTipBox.width /2;
		_noTipBox.visible = _showNoTipBox;
	}
	
	/**显示确定提示*/		
	private function showYes():void
	{
		addTarget(_alertBg);
		addTarget(_yesBtn,0,_btnY-_alertBg.height/2);
		_yesBtn.addEventListener(MouseEvent.CLICK,onClickYesHandler);
		addText();
	}
	
	/**显示选择提示*/		
	private function showYesNo():void
	{
		addTarget(_alertBg);
		addTarget(_yesBtn, -_yesBtn.width-10,_btnY-_alertBg.height/2);
		addTarget(_noBtn,10,_btnY-_alertBg.height/2);
		_yesBtn.addEventListener(MouseEvent.CLICK,onClickYesHandler);
		_noBtn.addEventListener(MouseEvent.CLICK,onClickNoHandler);
		addText();
	}
	
	/**点击确定按钮*/	
	private function onClickYesHandler(e:MouseEvent):void
	{
		_btnType = 0;
		_clickBtnCallBack(_btnType,this);
	}
	
	/**点击取消按钮*/		
	private function onClickNoHandler(e:MouseEvent):void
	{
		_btnType = 1;
		_clickBtnCallBack(_btnType,this);
	}
	
	/**添加文本*/		
	private function addText():void
	{
		_textInfo = TextFieldUtil.newTextField(null,_textinfoStr,
			{mouseEnabled:false,type:TextFieldType.DYNAMIC,filters:[new GlowFilter(0, 1, 2, 2, 4, 1)],width:_textWidth,wordWrap:true,autoSize:TextFieldAutoSize.CENTER},'#F5E49F');
		_textInfo.x = -_textWidth/2;
		_textInfo.y = _textY +(_textHeight -  _textInfo.height)/2 -_alertBg.height/2;
		if(_textInfo.y < -_alertBg.height/2+ _textY) _textInfo.y = -_alertBg.height/2+ _textY;
		this.addChild(_textInfo);
	}
	
	/**添加目标*/		
	private function addTarget(target:DisplayObject,x:int=0,y:int=0):void
	{
		target.x = x==0 ? -target.width/2:x;
		target.y = y==0 ? -target.height/2:y;
		this.addChild(target);
	}
	
	public function set clickBtnCallBack(value:Function):void{_clickBtnCallBack = value;}
	
	public function clear():void
	{
		if(_yesBtn) _yesBtn.addEventListener(MouseEvent.CLICK,onClickYesHandler);
		if(_noBtn) _noBtn.addEventListener(MouseEvent.CLICK,onClickNoHandler);
		while(this.numChildren) this.removeChildAt(0);
		_yesBtn = null;
		_noBtn = null;
		_textInfo = null;
		_alertBg = null;
		if(this.parent) this.parent.removeChild(this);
	}
}

class DrawShape extends Sprite
{
	private var _bgShape:Shape = new Shape();
	private var _titleShape:Shape = new Shape();
	private var _btnBgShape:Shape = new Shape();
	private var _matrix:Matrix;
	
	private var _bgWidth:int;
	private var _bgHeigth:int;
	public function DrawShape(bgWidth:int,bgHeigth:int){
		_bgWidth = bgWidth;
		_bgHeigth = bgHeigth;
		init();
	}
	
	private function init():void
	{
		this.addChild(_bgShape);
		this.addChild(_titleShape);
		this.addChild(_btnBgShape);
		draw();
	}
	
	private function draw():void
	{
		drawShape(_bgShape,_bgWidth,_bgHeigth,2,0xDFDFDF,1,0,1,0,0);
		drawShape(_titleShape,_bgWidth,20,2,0,0,0xDFDFDF,1,0,0);
		drawShape(_btnBgShape,_bgWidth,35,2,0,0,0xDFDFDF,1,0,0);
		_btnBgShape.y = _bgShape.height - _btnBgShape.height;
	}
	
	private function drawShape(target:*,width:int,heigth:int, lineThickness:Number = 1,lineColor:uint = 0,lineAlpha:Number = 1,color:uint = 0,alpha:Number = 1,ellipseWidth:int = 20,ellipseHeight:int= 20):void{
		target.graphics.clear();
		target.graphics.lineStyle(lineThickness,lineColor,lineAlpha);
		target.graphics.beginFill(color,alpha);
		target.graphics.drawRoundRect(0,0,width,heigth,ellipseWidth,ellipseHeight);
		target.graphics.endFill();
	}
}

class NoTipBox extends Sprite{
	private var _boxBitmap:Bitmap;
	private var _tickBitmap:Bitmap;
	private var _boxShape:Shape;
	private var _tickShape:Shape;
	private var _boxContain:Sprite = new Sprite();
	private var _tickContain:Sprite = new Sprite();
	private var _tipText:TextField;
	
	public function NoTipBox(box:BitmapData,tick:BitmapData)
	{
		if(box){//图片
			_boxBitmap = new Bitmap(box);
			_tickBitmap = new Bitmap(tick);
			addBoxShow(_boxBitmap,_tickBitmap);
		}else{//绘图
			_boxShape = drawBox();
			_tickShape = drawTick();
			addBoxShow(_boxShape,_tickShape);
		}
	}
	
	private function addBoxShow(box:DisplayObject,tick:DisplayObject):void{
		_boxContain.addChild(box);
		_tickContain.addChild(tick);
		this.addChild(_boxContain);
		this.addChild(_tickContain);
		_tickContain.y = -_tickContain.height / 4; 
		_tickContain.visible = false;
		_tipText = TextFieldUtil.newTextField(this,'不再提示',{x:_tickContain.x+_tickContain.width,mouseEnabled:false,filters:[new GlowFilter(0, 1, 2, 2, 4, 1)]},'#FFFF00');
		_tipText.width = _tipText.textWidth + 5;
		this.addEventListener(MouseEvent.CLICK,onClickHandler);
		this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStageHandler);
	}
	
	private function onRemoveToStageHandler(e:Event):void
	{
		this.removeEventListener(MouseEvent.CLICK,onClickHandler);
		this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStageHandler);
		while(this.numChildren) removeChildAt(0);
		_boxBitmap = null;
		_tickBitmap = null;
		_boxContain = null;
		_tickContain = null;
		_tipText = null;
	}
	
	private function onClickHandler(e:MouseEvent):void
	{
		Alert.neverNoTipFlag = _tickContain.visible = !_tickContain.visible;
		trace('neverNoTipFlag:' + Alert.neverNoTipFlag);
	}	
	
	private function drawBox():Shape
	{
		var shape:Shape = new Shape();
		shape.graphics.clear();
		shape.graphics.lineStyle(2,0xDFDFDF);
		shape.graphics.beginFill(0x666666);
		shape.graphics.drawRect(0,0,15,15);
		shape.graphics.endFill();
		return shape;
	}
	
	private function drawTick():Shape
	{
		var shape:Shape = new Shape();
		shape.filters = [new DropShadowFilter(4)];
		shape.graphics.beginFill(0x00FF00,1);
		shape.graphics.moveTo(-3, 8);
		shape.graphics.lineTo(5, 20);
		shape.graphics.lineTo(25, 0);
		shape.graphics.lineTo(6, 10);
		shape.graphics.lineTo(-3, 8);
		shape.graphics.endFill();
		return shape;
	}
	
	
	
}



